#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <memory>
#include "Thread.hpp"
using namespace std;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int ticket = 10000;
void* start_routine(void* args)
{   
    string name = static_cast<const char*>(args);
    while(1)
    {
        pthread_mutex_lock(&lock);
        if(ticket > 0)
        {
            usleep(1234);
            cout << name << "正在抢票" << "ticket: " << ticket << endl;
            ticket--;
            pthread_mutex_unlock(&lock);

        }
        else
        {
            pthread_mutex_unlock(&lock);
            break;
        }
        
    }
    return nullptr;
}
int main()
{
    // pthread_t tid1;
    // pthread_t tid2;
    // pthread_t tid3;

    // pthread_create(&tid1, nullptr, start_routine, (void*)"thread 1");
    // pthread_create(&tid2, nullptr, start_routine, (void*)"thread 2");
    // pthread_create(&tid3, nullptr, start_routine, (void*)"thread 3");


    // pthread_join(tid1, nullptr);
    // pthread_join(tid2, nullptr);
    // pthread_join(tid3, nullptr);




    shared_ptr<Thread> thread1(new Thread(start_routine, (void*)"user1", 1));
    shared_ptr<Thread> thread2(new Thread(start_routine, (void*)"user1", 2));
    shared_ptr<Thread> thread3(new Thread(start_routine, (void*)"user1", 3));


    thread1->join();
    thread2->join();
    thread3->join();


    
    return 0;
}